import time
from selenium.webdriver.common.by import By
from selenium import webdriver
# 打开浏览器
driver = webdriver.Chrome()
# 窗口最大化
driver.maximize_window()
driver.get('https://mail.163.com/')

# 添加登录需要的cookie
cookies = [
    {
        'name': 'NTES_SESS',
        'value': 'U7.ewroi4RHJGJxNCsLlY30NG8gvHbpYFAJAySzv0DbKtMe.tG1VzrkMJQI39ij3tNDnQgqbNexEVM0TvKZQ8WV8ia8Bvfm7RK1ICwx7Ypk32ajKzXGYHMMYL0zARVKCHXeTZEer8XZRCCfvyt.U1d0WG_qEJRfB7A3qWyyI4P2dSBflaJphpsL0jx.rKmAJZ0diIbuRBoM4r'
    },
    {
        'name': 'Coremail',
        'value': '194675855ecfe%VBjTbFjUMPrGXAmMFTUURsyBjWAslrLn%g1a91.mail.163.com'
    },
    {
        'name': 'MAIL_ENTRY_INFO',
        'value': '1|0|mail163|mail163_letter|183.94.29.201|fed3af66048440f8f66f0b5aba951833_v1|'
    },
    {
        'name': 'MAIL_ENTRY_CS',
        'value': 'b9af24ad5afd2f2496b660182b35d871'
    },
    {
        'name': 'cm_last_info',
        'value': 'dT15dWV5b25namlhbjc0JTQwMTYzLmNvbSZkPWh0dHBzJTNBJTJGJTJGbWFpbC4xNjMuY29tJTJGanM2JTJGbWFpbi5qc3AlM0ZzaWQlM0RWQmpUYkZqVU1QckdYQW1NRlRVVVJzeUJqV0FzbHJMbiZzPVZCalRiRmpVTVByR1hBbU1GVFVVUnN5QmpXQXNsckxuJmg9aHR0cHMlM0ElMkYlMkZtYWlsLjE2My5jb20lMkZqczYlMkZtYWluLmpzcCUzRnNpZCUzRFZCalRiRmpVTVByR1hBbU1GVFVVUnN5QmpXQXNsckxuJnc9aHR0cHMlM0ElMkYlMkZtYWlsLjE2My5jb20mbD0tMSZ0PS0xJmFzPXRydWU='
    },

]

for cookiet1 in cookies:
    driver.add_cookie(cookiet1)
# 刷新浏览器
time.sleep(2)
driver.refresh()
# 定位写信按钮
driver.find_element(By.ID, '_mail_component_149_149').click()
# 定位收件人框输入用户名
time.sleep(2)
driver.find_element(By.CLASS_NAME, 'nui-editableAddr-ipt').send_keys('Jerry_13237238803@163.com')
# 定位主题框输入主题
time.sleep(2)
driver.find_element(By.XPATH, '//header/div[2]/div[1]/div/div/input').send_keys('哈哈哈')
# 切换页面
frame = driver.find_element(By.CLASS_NAME, 'APP-editor-iframe')
driver.switch_to.frame(frame)
# 输入内容
time.sleep(2)
driver.find_element(By.CLASS_NAME, 'nui-scroll').send_keys('Hello world')
# 切换到主页面
driver.switch_to.default_content()
# 点击发送
time.sleep(2)
driver.find_element(By.XPATH, '//footer/div[1]/span[2]').click()
# 退出浏览器
time.sleep(5)
driver.quit()